package b2.widget
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.scene.guide.*;
	import b2.util.*;
	import b2.widget.vip.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.Point;
	import flash.net.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class PlayerPicIcon extends Sprite
	{
		private var expBar:ProgressBar;
		private var levelText:TextField;
		private var titleText:TextField;
		private var playerId:int;
//		private var playerMessage:PlayerMessage;
		private var userPicPaneBgr:Sprite;
		private var tankPicPaneBgr:Sprite;
//		private var expTip:BasicMessage;
		private var tankIcon:TankPicIcon;
				
		public function PlayerPicIcon(playerId:int)
		{
			super();
			this.playerId = playerId;
			var playerInfo:PlayerInfo = PlayerInfoManager.getInstance().players[playerId];
			//用户背景框
			var UserPicPaneBgr:Class = ResourceHelper.getInstance().getClassByName("UserPicPaneBgr");
			userPicPaneBgr = new UserPicPaneBgr();
			addChild(userPicPaneBgr);
			//获得用户头像
			if (playerInfo.figureurl != null)
			{
				var pictLdr:Loader = new Loader();
				var pictURLReq:URLRequest = new URLRequest(playerInfo.figureurl);
				pictLdr.load(pictURLReq);
				pictLdr.x = 16;
				pictLdr.y = 30;
				userPicPaneBgr.addChild(pictLdr);
			}
			//黄钻图标
			if (playerInfo.is_yellow_vip || playerInfo.is_yellow_year_vip)
			{
				var vipIcon:VipIcon = new VipIcon(playerInfo.yellow_vip_level, playerInfo.is_yellow_year_vip);
				vipIcon.x = 80;
				vipIcon.y = 2;
				addChild(vipIcon);
			}
			//用户名字
			var userName:TextField = new TextField();
        	userName.autoSize = TextFieldAutoSize.CENTER;
			userName.selectable = false;
			userName.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);
			userName.x = 45;
			userName.y = 3;
			userName.text = playerInfo.nickname.substr(0,4);
			userPicPaneBgr.addChild(userName);
			//经验条
			var percentage:Number = playerInfo.experience / Config.levelConfigs[playerInfo.level - 1].experience;
			var ExpBarFrame:Class = ResourceHelper.getInstance().getClassByName("ExpBarFrame");
			var ExpBar:Class = ResourceHelper.getInstance().getClassByName("ExpBar");
			expBar = new ProgressBar(ExpBarFrame, ExpBar, percentage, 0);
			expBar.x = 16;
			expBar.y = 82;
			addChild(expBar);
			//等级
			levelText = new TextField();
			levelText.selectable = false;
        	levelText.autoSize = TextFieldAutoSize.CENTER;
			levelText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x000000);
			levelText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			levelText.text = "等级: " + playerInfo.level.toString();
			levelText.x = 20;
			levelText.y = 98;
			addChild(levelText);
			expBar.addEventListener(MouseEvent.MOUSE_OVER, onExpMouseOver);
			expBar.addEventListener(MouseEvent.MOUSE_OUT, onExpMouseOut);
			//军衔
			titleText = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x0000FF, true);
			titleText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = playerInfo.titleName;
			titleText.x = 45 - titleText.width / 2;
			titleText.y = 116;
			addChild(titleText);
			refresh();
			titleText.addEventListener(MouseEvent.MOUSE_OVER, onTitleMouseOver);
			titleText.addEventListener(MouseEvent.MOUSE_OUT, onExpMouseOut);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		private function onAddedToStage(event:Event):void
		{
			refreshExpBar(null);
			//监听经验变动
			ServerHelper.getInstance().addEventListener(ServerEvent.END_FIGHT, refreshExpBar);
			ServerHelper.getInstance().addEventListener(ServerEvent.FETCH_GIFT, refreshExpBar);
			ServerHelper.getInstance().addEventListener(ServerEvent.ACCOMP_TASK, refreshExpBar);
		}
		
		private function onRemovedFromStage(event:Event):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.END_FIGHT, refreshExpBar);
			ServerHelper.getInstance().removeEventListener(ServerEvent.FETCH_GIFT, refreshExpBar);
			ServerHelper.getInstance().removeEventListener(ServerEvent.ACCOMP_TASK, refreshExpBar);
		}
		
		private function refreshExpBar(event:ServerEvent):void
		{
			var playerInfo:PlayerInfo = PlayerInfoManager.getInstance().players[playerId];
			//更新经验条
			expBar.percentage = playerInfo.experience / Config.levelConfigs[playerInfo.level - 1].experience;
			levelText.text = "等级: " + playerInfo.level.toString();
			titleText.text = playerInfo.titleName;
			titleText.x = 45 - titleText.width / 2;
			if (playerId == PlayerInfoManager.getInstance().myUserId && playerInfo.level > playerInfo.currentLevel
				&& !GuideScene.getInstance().enabled)
			{
				var upgradeDialog:UpgradeDialog = new UpgradeDialog();
				WidgetManager.getInstance().popOut([upgradeDialog], true, true);
				playerInfo.currentLevel = playerInfo.level;
			}
		}
		
		public function refresh():void
		{
			var playerInfo:PlayerInfo = PlayerInfoManager.getInstance().players[playerId];
			//更新坦克图标
			if (tankIcon != null && tankIcon.parent != null)
				tankIcon.parent.removeChild(tankIcon);
			tankIcon = new TankPicIcon(playerInfo.campTankId);
			tankIcon.x = 80;
			tankIcon.y = 95;
			addChild(tankIcon);
		}
		
		private function onTitleMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			var playerInfo:PlayerInfo = PlayerInfoManager.getInstance().players[playerId];
			var globalPoint:Point = expBar.parent.localToGlobal(new Point(titleText.x, titleText.y));
			var titleTip:BasicMessage = new BasicMessage("荣誉: " + playerInfo.honour + "/" + Config.levelConfigs[playerInfo.title - 1].honour);
			titleTip.x = globalPoint.x + 80;
			titleTip.y = globalPoint.y;
			WidgetManager.getInstance().popOutMouseOverMessage(titleTip);
		}
		
		private function onExpMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			var playerInfo:PlayerInfo = PlayerInfoManager.getInstance().players[playerId];
			var globalPoint:Point = expBar.parent.localToGlobal(new Point(expBar.x, expBar.y));
			var expTip:BasicMessage = new BasicMessage("经验: " + playerInfo.experience + "/" + Config.levelConfigs[playerInfo.level - 1].experience);
			expTip.x = globalPoint.x + 80;
			expTip.y = globalPoint.y;
			WidgetManager.getInstance().popOutMouseOverMessage(expTip);
		}
		
		private function onExpMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			WidgetManager.getInstance().closeMouseOverMessage();
		}
	}
}